
/* + Description: Visualize/animate data structures like binary tree 
 * and related operations (insert, delete etc.) 
 * Use of various classes in OSG is demonstrated.
 *
 * + Author: Vijay C Patil
 * + Date: July 2008
 *
 * */

#include<osgViewer/Viewer>
#include<osg/Geode>
#include<osg/Group>
#include<osg/NodeCallback>
#include <string>
#include <iostream>
#include <vector>
#include "btnode.h"
#include "scenegraph.h"
#include "callback.h"
#include "global.h"

using namespace std;

/* Entry point. Create and populate scene graph */
int main(int argc, char * argv[])
{
	osgViewer::Viewer viewer;
	DataContainer input;
	int number;
	BinaryTreeType tree_type; // Simple or Red-black 

	process_arguments(argc, argv, tree_type);
	cout << "Enter Input (-1 to end): ";
	cin >> number;
	while(number != -1) {
		input.push_back(number);
		cin >> number;
	}

	/* Draw and animate binary search tree using root node. */
	viewer.setUpViewInWindow(0, 0, 1000, 800);

	/* ?? SG has just one geode. This geode has node callback. */
    osg::ref_ptr<osg::Geode> geode_input = new osg::Geode;
    osg::ref_ptr<osg::Geode> geode_tree = new osg::Geode;
    osg::ref_ptr<osg::Group> group = new osg::Group;

	// 1. Input related geode.
	geode_input = draw_input(input);
	group->addChild(geode_input.get());

	// 2. Binary tree related geode, which is empty now.
	group->addChild(geode_tree.get());

    group->setUpdateCallback(new CBTreeInsert(input, tree_type));

	viewer.setSceneData(group.get());

	osg::Vec3d eye(0.0, -60.0, -10.0);
	osg::Vec3d target(0.0, 0.0, -10.0);
	osg::Vec3d up(0.0, 0.0, 1.0);

	/* Animation loop. */
	while(viewer.done() == 0)
	{
		viewer.getCamera()->setViewMatrixAsLookAt(eye, target, up);
		viewer.getCamera()->setClearColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));

		viewer.frame();
	}

	//return viewer.run();
	cout << "Animation successfully completed. " << endl;

	return 0;
}

